package net.zoneland.knowledge.utils;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import net.zoneland.knowledge.enums.DiYiYiTiReportColumnEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.List;

/**
 * 第一议题列宽handler.
 * @author zx
 */
public class DiYiYiTiReportColumnHandler extends AbstractColumnWidthStyleStrategy {

    /**
     * 列宽计算逻辑：以下标为1的列的宽度计算逻辑说明下，正常情况下设置的列宽是33乘以256，但实际上我们生成的excel中列宽是32.22，
     * 因为字体样式、大小以及单元格边框等占用额外的像素，所以实际的列宽比代码中设置的列宽要小0.78英寸（excel中的列宽单位是英寸），
     * 代码中设置的33也是英寸单位，那么实际我们需要设置成33.78英寸，但33.78乘以256是Double类型的数值，setColumnWidth的第二个参数必须是整数，所以在33.78*256=8647.68的基础上向上取整为8648
     * @param writeSheetHolder
     * @param cellDataList
     * @param cell
     * @param head
     * @param integer
     * @param isHead 是否头
     */
    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer integer, Boolean isHead) {
        final boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
        if (needSetWidth) {
            final Sheet sheet = writeSheetHolder.getSheet();
            final Integer index = cell.getColumnIndex();
            try {
                sheet.setColumnWidth(index, Integer.valueOf(DiYiYiTiReportColumnEnum.getByIntKey(index).getStringKey()));
            } catch (Exception e) {
                sheet.setColumnWidth(index, 2816);
            }
        }
    }
}
